<?php
namespace app\index\controller;
use think\Controller;
use think\Db;

class Code extends Controller
{
   public function imageCode()
   {
            session_start();
            $image=imagecreatetruecolor(150, 49);
            $bgcolor=imagecolorallocate($image, 255, 255, 255); 
            $textcolor=imagecolorallocate($image,0,0,255);
            imagefill($image, 0, 0, $bgcolor);
            $captch_code="";
            //该循环,循环取数
            for($i=0;$i<4;$i++)
            {
                $fontsize=6;
                $x=($i*25)+rand(5,10);
                $y=rand(5,10);
                //$fontcontent=$i>2?chr(rand(97,122)):chr(rand(65,90));//是小写，否则是大写
                $data='ABCDEFGHKabcdeghjkpqrstuvwxyz23456789';//我只想随机这些数= = 啦啦啦
                $fontcontent=substr($data,rand(0,strlen($data)-1),1);
                $fontcolor=imagecolorallocate($image,rand(0,100),rand(0,100),rand(0,100));
                imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor); //水平地画一行字符串
                $captch_code.=$fontcontent;
            }
            //该循环,循环画背景干扰的点
            for($m=0;$m<=600;$m++)
            {
                $x2=rand(1,99);
                $y2=rand(1,99);
                $pointcolor=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));
                imagesetpixel($image,$x2,$y2,$pointcolor);// 水平地画一串像素点
            }
            //循环画干扰直线
            for ($i=0;$i<=10;$i++){
                $x1=rand(0,99);
                $y1=rand(0,99);
                $x2=rand(0,99);
                $y2=rand(0,99);
                $linecolor=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));
                imageline($image,$x1,$y1,$x2,$y2,$linecolor);//画一条线段
            }
            header('content-type:image/png');
            imagepng($image);
            //销毁
            imagedestroy($image);
            $_SESSION['authcode']=strtolower($captch_code);//将变量保存再session的authcode变量中
    }
    
    public function getValue()
    {
        session_start();
        if(!isset($_SESSION['authcode'])){
           $ut  = new Utiltool(null);
           return $ut->getpdata();
        }
        $ut  = new Utiltool($_SESSION['authcode']);
          return $ut->getpdata();
       
    }
}
?>